<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 10.06.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class clientNodeSearch
    ****
    * This classs represents a root page of any solution
    ****
    * @package clientNodes
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 10.06.2005
    * @copyright essentialmind.com 2005
    ***/
    class clientNodeSearch extends clientNode{

        /**
         * Process inputs
         * 
         * @return void 
         **/
        public function process(){
            //turn caching off (temporarily, this has to be fixed somehow)
            $this->smarty->caching = false;
            
            //mark the search stack
            $this->crumbs->push('Search',null);
            
            //prepare the search string for a valid UTF string
            $word = encodingHandler::unescape($_GET['search']);

            
                $string   = str_split($word);
                $toReturn = array();
                foreach($string as $key => $val){
                    $t = ord($val);
                    switch($t){
                        case 195: $toReturn[] = '&ouml;'; break;
                        case 182: $toReturn[] = ''; break;
                        case 252: $toReturn[] = '&uuml;'; break;
                        case 246: $toReturn[] = '&ouml;'; break;
                        case 228: $toReturn[] = '&auml;'; break;
                        default:  $toReturn[] = $val;
                    }
          
                    //echo ord($val).'-'.$val.' ';
                }
                $word   = implode('',$toReturn);
            //echo $word;
            //echo ord($word{3});exit;
            
            $word = str_replace(';',' ',$word);

            //first make sure the word fits ;)
            $word = trim(strip_tags($word));
            
            $word = strtolower($word);
            
            //now we search the index only if the search string is long enough
            if(strlen($word)>=3){
                /*
                    The line below is to be outsourced to a special token 
                    processor. In fact this whole controller needs careful
                    redesign to match different use requests
                */
                $filter = "WHERE si.longindex LIKE '%".$word."%' AND pm.published IS NOT NULL ";
                
                //are we searching a specific folder set?
                if(!isset($_GET['all']) and $this->db->getOne("SELECT parent_id FROM structure WHERE id = ".$this->node)){
                    $myTree     = new tree($this->db,'structure',$this->log);
                    $nodes      = $myTree->getChildren($this->node);
                    $children[] = $this->node;  //add current node to the set limiter
                    
                    //quick fix, needs outsourcing
                    foreach($nodes as $node){
                        $children[] = $node['id'];
                    }
                
                    $filter    .= ' AND pm.nodeid IN ('.implode(',',$children).')';
                }
                
                //$this->db->debug=1;
                
                /*
                    Fetching the actual data. As already noted before, this search
                    tool is Listing Solution specific, a better tool needs to be
                    developed for universal site indexing and access
                */
                
                $data = $this->db->getAll("SELECT 
                                                pm.created          AS intime,
                                                pm.title            AS title,
                                                ni.*,
                                                get_path(pm.nodeid)||pm.pageurl||'.html' AS url,
                                                'files/pages/101__'||ni.id||'/' AS image_path
                                           FROM searchindex AS si 
                                           LEFT JOIN pagemap AS pm ON si.pageid = pm.id
                                           LEFT JOIN node_shop_article AS ni ON pm.pageid = ni.id
                                           ".$filter."
                                           ORDER BY pm.published DESC
                                           LIMIT ".$GLOBALS['_CONFIG']['perpage']." OFFSET ".$_GET['offset']);
                
                
                /*
                    create the pager for this iFolder, in order to do this we need to
                    first identify the total number of inserats there are in the
                    currently selected folder
                */
                $total = $this->db->getOne("SELECT count(*)
                                               FROM searchindex AS si 
                                               LEFT JOIN pagemap AS pm ON si.pageid = pm.id
                                               LEFT JOIN node_shop_article AS ni ON pm.pageid = ni.id
                                               ".$filter);
                //$items = $this->db->getAll("SELECT * FROM searchindex LIMIT 10");
                //print_r($items);
                //create pager
                $pager = new pager($total,$GLOBALS['_CONFIG']['perpage'],$_GET['pg']);
                
                //echo $filter;    
                //pass data to smarty
                $this->smarty->assign(array(
                                        'items'    => $data,
                                        'total'    => $total,
                                        'pager'    => $pager->getPager()));
            }
        }

        /**
         * set the action template to search.html
         * 
         * @return void
         **/
        public function setTemplate(){
            $this->setActionTemplate('search.html');
        }
    }
?>